import request from "@/utils/request";
import axios from "axios";
import { getToken } from "xqjr-module-auth";
import { notification } from "ant-design-vue";

// 分享二维码
export function getQRCodeByrecruitInfoIds(data) {
  return request({
    url: "/recruit2/api/v1/recruitInfos/getQRCodeByrecruitInfoIds",
    method: "post",
    data
  });
}

export function getNormalPost(params) {
  return request({
    url: "/recruit2/api/v1/recruitInfos/getNormalPost",
    method: "GET",
    params
  });
}

const f = (res, name) => {
  if (res.data) {
    const rspHeaders = res.headers;
    const blob = new Blob([res.data], { type: "application/octet-stream" });
    const link = document.createElement("a");
    link.href = URL.createObjectURL(blob);
    document.body.appendChild(link);
    if (rspHeaders["content-disposition"]) {
      try {
        const fileName = decodeURI(
          rspHeaders["content-disposition"]
            .split(";")[1]
            .split("=")[1]
            .replaceAll('"', "")
        );
        const fileNameArr = fileName.split(".");
        link.download = name + "." + fileNameArr[1];
        link.click();
        URL.revokeObjectURL(link.href);
        document.body.removeChild(link);
      } catch (err) {
        console.log("error log --> ", err);
      }
    } else {
      notification.error({ message: "下载失败" });
    }
  }
};

export function onPostDownload(url, name) {
  axios({
    url: "/file/api/v1/site/download",
    method: "post",
    data: {
      siteFileUrls: [url]
    },
    responseType: "blob",
    headers: {
      token: getToken()
    }
  })
    .then(res => {
      f(res, name);
    })
    .catch(err => {
      console.log("error log --> ", err);
    });
}
export function onGetDownload(url, name) {
  axios({
    url: "/recruit2/api/v1/talentPools/downloadOss",
    method: "get",
    params: {
      url
    },
    responseType: "blob",
    headers: {
      token: getToken()
    }
  })
    .then(res => {
      f(res, name);
    })
    .catch(err => {
      console.log("error log --> ", err);
    });
}
